﻿using System;
using System.Windows;
using Data;
using View;
using View.EventArguments;

namespace CSE
{
    public class MainDispatcher
    {
        private static MainDispatcher _mainDispatcher;
        private readonly MainWindow _mainWindow;
        private readonly DataDispatcher _dataDispatcher;

        private MainDispatcher()
        {
            _dataDispatcher = DataDispatcher.GetInstance();
            _mainWindow = new MainWindow();
            // <MainWindowListeners>
            _mainWindow.TryLoginEvent += TryLoginEventHandler;
            // </MainWindowListeners>

            _mainWindow.ShowLoginPanel();
            _mainWindow.Show();
        }

        private void TryLoginEventHandler(object sender, TryLoginEventAgrs e)
        {
            // Берётся хэш-код введённого пароля
            if (_dataDispatcher.TryLogin(e.Username, e.Password.GetHashCode().ToString()))
                try{_mainWindow.ShowMainWorkSpace();}
                catch (Exception exception)
                { MessageBox.Show(exception.ToString()); }
            else
                _mainWindow.ShowMessageLoginFail();
        }

        public static MainDispatcher GetInstance()
        {
            return _mainDispatcher ?? (_mainDispatcher = new MainDispatcher());
        }
    }
}